home *** CD-ROM | disk | FTP | other *** search
- unit Unikeyu4;
-
- (*
- TUniKey demonstration program.
- Copyright 1995, Demosphere International, Inc.
-
- This program exercises the UniKey component in a stand-alone or
- multi-user environment. It surfaces most of the options a programmer
- can control into a convenient form interface.
-
- This program can only be recompiled if you have the UNIKEY.DCU or
- UNIKEY.PAS files, or place the UniKey component on the form.
-
- You can try your own key table with this demo. Just create a Paradox
- .DB file, structuring it with one or more numeric fields. Edit this
- file and create one record with the starting key values in each field.
- For example: Customer=1001, Order=250, Item=701.
- Then in the demo program, each time you ask for a new key, refer to
- the desired field by its number: customer key is field 0, etc.
- Note that only the first record is used to store all keys.
-
- For a network-based test, place the key table in a directory that all
- workstations have read/write access to, and then run the demo program
- on several workstations. Use the Beep and Continuous testing options.
- This will let you see how often collisions occur, and let you gauge
- the best value for MaxRetries.
-
- *)
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, DB, DBTables, StdCtrls, Unikey, ExtCtrls;
-
- type
- TForm1 = class(TForm)
- Button1: TButton;
- Button2: TButton;
- GroupBox1: TGroupBox;
- Edit3: TEdit;
- Edit4: TEdit;
- Label6: TLabel;
- Label7: TLabel;
- GroupBox2: TGroupBox;
- CheckBox1: TCheckBox;
- CheckBox2: TCheckBox;
- CheckBox3: TCheckBox;
- Label4: TLabel;
- Edit1: TEdit;
- Label5: TLabel;
- Edit2: TEdit;
- GroupBox3: TGroupBox;
- Label1: TLabel;
- Label2: TLabel;
- Label3: TLabel;
- Label8: TLabel;
- Edit5: TEdit;
- Bevel1: TBevel;
- Bevel2: TBevel;
- Bevel3: TBevel;
- CheckBox4: TCheckBox;
- Label9: TLabel;
- Button3: TButton;
- procedure Button1Click(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- procedure Button3Click(Sender: TObject);
- private
- { Private declarations }
- UniKey1:TUniKey; {remove if component is placed on form}
- goodkeys:longint;
- FRunning:boolean;
- procedure SetRunning(const Running:boolean);
- procedure UniKey1MaxRetries(Sender:TObject; var KeepTrying:boolean);
- procedure UniKey1NewKey(Sender: TObject);
- property Running:boolean read FRunning write SetRunning;
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
- Uses dbitypes, Dialogs, UniKeyA4;
-
- {$R *.DFM}
-
- function NumKeyToAlpha(const i:longint):string;
- {
- This is an example of changing a sequential numerical key value into a
- special alphanumeric key. The AllowableChars constant gives the
- "collating sequence" of the characters that are allowed to be used.
- Each "digit" of the result is one of the characters from this string
- only. When the last character is used in any "digit" position, the next
- higher "digit" is incremented in the result.
- }
- const
- AllowableChars='abcdefGHIJK123456';
- x:string[length(AllowableChars)]=AllowableChars;
- var
- m:integer;
- j:longint;
- k:longint;
- r:integer;
- begin
- Result:='';
- m:=length(AllowableChars);
- j:=i;
- repeat
- r:=j mod m; {remainder gives "digit" to be used in result}
- j:=j div m; {amount left for higher place "digits"}
- Result:=x[r+1]+Result;
- until (j=0) or (length(Result)>10);
- end; {NumKeyToAlpha}
-
- procedure TForm1.SetRunning(const Running:boolean);
- {adjust all visible controls for "running" state}
- begin
- if FRunning<>Running then begin
- FRunning:=Running;
- Button1 .Enabled:=not Running;
- Button2 .Enabled:=not Running;
- Button3 .Enabled:=not Running;
- CheckBox1.Enabled:=not Running;
- CheckBox3.Enabled:=not Running;
- CheckBox4.Enabled:=not Running;
- end;
- end; {SetRunning}
-
- procedure TForm1.UniKey1MaxRetries(Sender:TObject; var KeepTrying:boolean);
- {Example of a user-defined MaxRetries event handler}
- begin
- KeepTrying:=MessageDlg((Sender as TUniKey).TableName+':'#13+
- 'Table may be temporarily locked by another user.'#13+
- 'Press OK to try again, '#13+
- 'or Cancel to terminate request for new key.',
- mtInformation,[mbOK,mbCancel],0)=IDOK;
- end; {UniKey1MaxRetries}
-
- procedure TForm1.Button1Click(Sender: TObject);
- var
- success:boolean;
- status:string;
-
- procedure ShowStatus;
- begin
- if status='' then
- case success of
- true :status:='ok';
- false:status:='unknown failure';
- end;
- Label1.Caption:='Status: '+status;
- end; {ShowStatus}
-
- begin
- if Running then exit; {prevent any undesired reentry from extra click messages}
- Running:=true;
- success:=false;
- status:='';
- UniKey1:=TUniKey.Create(Self); {remove if component on form}
- try try
- {Override most UniKey properties with values set on form}
- with UniKey1 do begin
- DatabaseName:=Edit3.Text;
- TableName :=Edit4.Text;
- FieldNo :=StrToInt(Edit2.Text);
- MaxRetries :=StrToInt(Edit5.Text);
- OnMaxRetries:=UniKey1MaxRetries; {user-defined MaxRetries event handler}
- BeepOnLock :=CheckBox1.Checked;
- Reserve :=StrToInt(Edit1.Text);
- if CheckBox3.Checked then
- OnNewKey:=UniKey1NewKey {user-defined NewKey event handler}
- else
- OnNewKey:=nil; {remove any user-defined event handler}
- end;
- repeat {for continuous testing, until checkbox is not checked}
- if CheckBox2.Checked then
- CheckBox2.Caption:='Test continuously (click to stop)';
- try
- success:=false;
- {the calls to NewKey in the next statement actually obtain the key}
- if CheckBox4.Checked then
- Label2.Caption:='New key: '+NumKeyToAlpha(UniKey1.NewKey)
- else
- Label2.Caption:='New key: '+IntToStr(UniKey1.NewKey);
- {any exception raised by NewKey will skip next lines}
- with UniKey1 do begin
- Open;
- Label9.Caption:='Key Field: '+Fields[FieldNo].FieldName;
- Close;
- end;
- success:=true;
- inc(goodkeys); {count number of keys successfully obtained}
- except
- {eat this exception because we already used an OnMaxRetries handler}
- on EUniKeyMaxRetries do
- status:='Max retries';
- end;
- ShowStatus;
- Label3.Caption:='Keys obtained: '+IntToStr(goodkeys);
- Application.ProcessMessages;
- until not CheckBox2.Checked; {for continuous testing}
- except
- on E:Exception do
- if status='' then
- status:=E.Message
- end;
- finally
- ShowStatus;
- UniKey1.Free; {remove if component on form}
- CheckBox2.Caption:='Test continuously';
- CheckBox2.Checked:=false;
- Running:=false;
- end;
- end; {Button1Click}
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- goodkeys:=0; {prepare to count number of keys successfully obtained}
- end;
-
- procedure TForm1.UniKey1NewKey(Sender: TObject);
- {Example of custom NewKey handler - this one causes decrementing keys to
- be generated instead of the default increment-by-one scheme}
- begin
- with UniKey1 do
- NewKey:=OldKey-1;
- end; {UniKey1NewKey}
-
- procedure TForm1.Button2Click(Sender: TObject);
- begin
- Close
- end;
-
- procedure TForm1.Button3Click(Sender: TObject);
- begin
- AboutBox.ShowModal;
- end;
-
- end.
-